package com.example.demo.controller;

import com.example.demo.dao.UserDao;
import com.example.demo.model.Car;
import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.*;

@RestController
@RequestMapping(value = "/try")
@EnableAutoConfiguration
public class UserController {
    @Autowired
    private UserDao userDAO;
    @GetMapping(value = "/user")
    public List<User> getUsers(){
        return userDAO.selectUsers();
    }
    @DeleteMapping(value = "/user/{id}")
    public String deleteUsers(@PathVariable int id){
        try{
            userDAO.deleteUsers(id);
            return "success";
        }catch (Exception e){
            return "fail";
        }
    }
    @GetMapping(value = "/user/{id}")
    public List<User> getUser(@PathVariable int id){
        return userDAO.selectUser(id);
    }
    @PutMapping(value = "/user")
    public String updateUsers(User user){
        System.out.println(user.toString());
        try{
            userDAO.updateUser(user);
            return "success";
        }catch (Exception e){
            return "fail";
        }

    }
    @PostMapping(value = "/user")
    public String addUser(User user){
        try{
            System.out.println(user.toString());
            userDAO.addUser(user);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }

    }



}
